package crypto

import (
	"bytes"

	"golang.org/x/crypto/cryptobyte"
	"golang.org/x/crypto/cryptobyte/asn1"
)

// Cryptobyte 编解码
func Cryptobyte(source []byte) []byte {
	// var b cryptobyte.Builder
	var buffer []byte
	// b := cryptobyte.NewBuilder(buffer)
	b := cryptobyte.NewFixedBuilder(buffer)

	b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
		b.AddASN1(asn1.INTEGER, func(b *cryptobyte.Builder) {
			b.AddASN1(asn1.INTEGER, func(b *cryptobyte.Builder) {
				b.AddBytes([]byte{0x01})
			})
		})
		b.AddASN1(asn1.OCTET_STRING, func(b *cryptobyte.Builder) {
			b.AddBytes(source)
		})
	})

	// return b.Bytes()
	return b.BytesOrPanic()

	// b.MarshalASN1()
	// b.Unwrite()
}

func pad(src []byte, blockSize int) []byte {
	padding := blockSize - len(src)%blockSize
	padText := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(src, padText...)
}
func unpad(src []byte) []byte {
	padding := int(src[len(src)-1])
	return src[:len(src)-padding]
}
